{extend name="./view/base/admin.html"}
{block name="body"}
<ul class="nav nav-tabs">
    {foreach $cate_list as $k=>$v}
    {if count($v['config'])}
    <li class="nav-item">
        <a href="{:addons_url('web/config/index',['cate_id'=>$v['id']])}"
            class="nav-link {if $Request.get.cate_id==$v.id}active{elseif !$Request.get.cate_id&&$k==0}active{/if}">{$v.name}</a>
    </li>
    {/if}
    {/foreach}
</ul>
<div class="tab-content">
    <!-- begin tab-pane -->
    <div class="tab-pane fade active show">
        <form id="form">
            {foreach $config_list as $k=>$v}
            <div class="form-group">
                <label>{$v.name}&nbsp;<code>config('{$v.cate.alias}.{$v.key}')</code></label>
                <input type="hidden" name="data[{$k}][id]" value="{$v.id}">
                <input type="hidden" name="data[{$k}][type]" value="{$v.type}">
                {if $v.type=='text'}
                <input type="text" class="form-control" name="data[{$k}][value]" value="{$v.value}">
                {elseif $v.type=='textarea'}
                <textarea name="data[{$k}][value]" class="form-control" rows="10">{$v.value|raw}</textarea>
                {elseif $v.type=='ueditor'}
                <script id="editor{$k}" name="data[{$k}][value]"
                    type="text/plain">{$v.value|htmlspecialchars_decode|raw}</script>
                {elseif $v.type=='radio'}<br>
                {foreach $v.options as $k1=>$v1}
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="radio" name="data[{$k}][value]" id="radio{$k}{$k1}" value="{$v1.key}" {if $v.value==$v1.key}checked{/if}/>
                    <label class="form-check-label" for="radio{$k}{$k1}">{$v1.name}</label>
                </div>
                {/foreach}
                {elseif $v.type=='checkbox'}<br>
                {foreach $v.options as $k1=>$v1}
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" name="data[{$k}][value]" id="checkbox{$k}{$k1}" value="{$v1.key}" {if in_array($v1.key,explode(',',$v.value))}checked{/if}/>
                    <label class="form-check-label" for="checkbox{$k}{$k1}">{$v1.name}</label>
                </div>
                {/foreach}
                {elseif $v.type=='image'}
                <div class="input-group">
                    <input type="text" class="form-control" name="data[{$k}][value]" value="{$v.value}" readonly="">
                    <div class="input-group-append">
                        <input type="file" class="d-none" onchange="upload(this)">
                        <button type="button" onclick="$(this).prev('input').click()" class="btn btn-yellow no-caret">上传图片</button>
                    </div>
                    <div class="input-group {if !$v.value}d-none{/if}">
                        <img src="{$v.value}" class="img-responsive img-thumbnail" width="150">
                        <em class="close ml-0" title="删除这张图片" style="cursor: pointer;" onclick="deleteImage(this)">×</em>
                    </div>
                </div>
                {elseif $v.type=='select'}
                <select name="data[{$k}][value]" class="form-control">
                    {foreach $v.options as $k1=>$v1}
                    <option value="{$v1.key}" {$v.value==$v1.key?'selected':''}>{$v1.name}</option>
                    {/foreach}
                </select>
                {/if}
                {if $v.desc}
                {if in_array($v.type,['radio','checkbox'])}
                <br>
                {/if}
                <small class="text-pink">{$v.desc}</small>
                {/if}
            </div>
            {/foreach}
            <button class="btn btn-info">提交</button>
        </form>
    </div>
</div>
{/block}
{block name="script"}
<script src="/static/ueditor/ueditor.config.js"></script>
<script src="/static/ueditor/ueditor.all.js"></script>
<script>
    {foreach $config_list as $k=>$v}
        {if $v['type']=='ueditor'}
            UE.getEditor('editor{$k}',{
                initialFrameHeight:450
            });
        {/if}
    {/foreach}
    $('#form').on('submit',function(){
        $.submit('',$(this).serializeJSON())
        return false;
    })
    function upload(obj){
        var data = new FormData();
        data.append('image',obj.files[0]);
        $.upload('{:url("upload")}',data,function(res){
            if(res.code==0){
                $.alert(res);
                return false;
            }
            $(obj).parent('div').prev('input').val(res.data);
            $(obj).parent('div').next('div').find('img').attr('src',res.data)
            $(obj).parent('div').next('div').removeClass('d-none')
        })
    }
    function deleteImage(obj){
        $(obj).parent('div').prev('div').prev('input').val('');
        $(obj).parent('div').addClass('d-none')
    }
</script>
{/block}